home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / sossnt.zip / SOSSNT / RPC / SVC_UDP.C < prev    next >
C/C++ Source or Header  |  1993-04-03  |  7KB  |  264 lines

  1. /*
  2.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  3.  * unrestricted use provided that this legend is included on all tape
  4.  * media and as a part of the software program in whole or part.  Users
  5.  * may copy or modify Sun RPC without charge, but are not authorized
  6.  * to license or distribute it to anyone else except as part of a product or
  7.  * program developed by the user.
  8.  * 
  9.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  10.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  11.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  12.  * 
  13.  * Sun RPC is provided with no support and without any obligation on the
  14.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  15.  * modification or enhancement.
  16.  * 
  17.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  18.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  19.  * OR ANY PART THEREOF.
  20.  * 
  21.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  22.  * or profits or other special, indirect and consequential damages, even if
  23.  * Sun has been advised of the possibility of such damages.
  24.  * 
  25.  * Sun Microsystems, Inc.
  26.  * 2550 Garcia Avenue
  27.  * Mountain View, California  94043
  28.  */
  29. #ifndef lint
  30. static char sccsid[] = "@(#)svc_udp.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  31. #endif
  32.  
  33. /*
  34.  * svc_udp.c,
  35.  * Server side for UDP/IP based RPC.  (Does some caching in the hopes of
  36.  * achieving execute-at-most-once semantics.)
  37.  *
  38.  * Copyright (C) 1984, Sun Microsystems, Inc.
  39.  */
  40.  
  41. #include <stdlib.h>
  42. #include <stdio.h>
  43. #include <malloc.h>
  44. #include <winsock.h>
  45. #include <io.h>
  46. #include "types.h"
  47. /* #include "in.h" */
  48. #include <errno.h>
  49. #include "xdr.h"
  50. #include "auth.h"
  51. #include "clnt.h"
  52. #include "rpc_msg.h"
  53. #include "svc.h"
  54.  
  55.  
  56. #define rpc_buffer(xprt) ((xprt)->xp_p1)
  57. #define MAX(a, b)     ((a > b) ? a : b)
  58.  
  59. static bool_t        svcudp_recv();
  60. static bool_t        svcudp_reply();
  61. static enum xprt_stat    svcudp_stat();
  62. static bool_t        svcudp_getargs();
  63. static bool_t        svcudp_freeargs();
  64. static void        svcudp_destroy();
  65.  
  66. static struct xp_ops svcudp_op = {
  67.     svcudp_recv,
  68.     svcudp_stat,
  69.     svcudp_getargs,
  70.     svcudp_reply,
  71.     svcudp_freeargs,
  72.     svcudp_destroy
  73. };
  74.  
  75. extern int errno;
  76.  
  77. /*
  78.  * kept in xprt->xp_p2
  79.  */
  80. struct svcudp_data {
  81.     u_int   su_iosz;    /* byte size of send.recv buffer */
  82.     u_long    su_xid;        /* transaction id */
  83.     XDR    su_xdrs;    /* XDR handle */
  84.     char    su_verfbody[MAX_AUTH_BYTES];    /* verifier body */
  85. };
  86. #define    su_data(xprt)    ((struct svcudp_data *)(xprt->xp_p2))
  87.  
  88. /*
  89.  * Usage:
  90.  *    xprt = svcudp_create(sock);
  91.  * * If sock<0 then a socket is created, else sock is used.
  92.  * If the socket, sock is not bound to a port then svcudp_create
  93.  * binds it to an arbitrary port.  In any (successful) case,
  94.  * xprt->xp_sock is the registered socket number and xprt->xp_port is the
  95.  * associated port number.
  96.  * Once *xprt is initialized, it is registered as a transporter;
  97.  * see (svc.h, xprt_register).
  98.  * The routines returns NULL if a problem occurred.
  99.  */
  100. SVCXPRT *
  101. svcudp_bufcreate(sock, sendsz, recvsz, port)
  102.     SOCKET sock;
  103.     u_int sendsz, recvsz;
  104.     u_short port;
  105. {
  106.     bool_t madesock = FALSE;
  107.     register SVCXPRT *xprt;
  108.     register struct svcudp_data *su;
  109.  
  110. #ifdef OLDSTUFF
  111.     struct sockaddr_in addr;
  112.     if (sock_getsockaddr(sock, &addr) == -1) {
  113.         (void) fprintf(stderr, "svcudp: cannot get socket addr\n");
  114.         (void) closesocket(sock);
  115.         return NULL;
  116.     }
  117. #endif
  118.     xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
  119.     if (xprt == NULL) {
  120.         fprintf(stderr, "svcudp_create: out of memory\n");
  121.         (void) closesocket(sock);
  122.         return (NULL);
  123.     }
  124.     su = (struct svcudp_data *)mem_alloc(sizeof(*su));
  125.     if (su == NULL) {
  126.         fprintf(stderr, "svcudp_create: out of memory\n");
  127.         (void) closesocket(sock);
  128.         return (NULL);
  129.     }
  130.     su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4;
  131.     if ((rpc_buffer(xprt) = malloc(su->su_iosz)) == NULL) {
  132.         fprintf(stderr, "svcudp_create: out of memory\n");
  133.         return (NULL);
  134.     }
  135.     xdrmem_create(
  136.         &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
  137.     xprt->xp_p2 = (caddr_t)su;
  138.     xprt->xp_verf.oa_base = su->su_verfbody;
  139.     xprt->xp_ops = &svcudp_op;
  140.     xprt->xp_port = port;    /* used to be: ntohs(addr.sin_port); */
  141.     xprt->xp_sock = sock;
  142.     xprt_register(xprt);
  143.     return (xprt);
  144. }
  145.  
  146. SVCXPRT *
  147. svcudp_create(sock, sz,port)    /* modification - create big size if sz == 1 */
  148. SOCKET sock;
  149. int sz;
  150. u_short port;
  151. {
  152.     if (sz == 1)
  153.         return svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE, port);
  154.     else
  155.         return svcudp_bufcreate(sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE, port);
  156. }
  157.  
  158. static enum xprt_stat
  159. svcudp_stat(xprt)
  160.     SVCXPRT *xprt;
  161. {
  162.  
  163.     return (XPRT_IDLE); 
  164. }
  165.  
  166. static bool_t
  167. svcudp_recv(xprt, msg)
  168.     register SVCXPRT *xprt;
  169.     struct rpc_msg *msg;
  170. {
  171.     struct svcudp_data *su = su_data(xprt);
  172.     XDR *xdrs = &(su->su_xdrs);
  173.     int rlen;
  174.     int raddr_len;
  175.  
  176.     again:
  177.     xprt->xp_addrlen = sizeof(struct sockaddr_in);
  178.  
  179. #ifdef OLDSTUFF
  180.     rlen = sock_recv(xprt->xp_sock, rpc_buffer(xprt), su->su_iosz, 0,
  181.                  &(xprt->xp_raddr));
  182. #endif
  183.     rlen = recvfrom(xprt->xp_sock, rpc_buffer(xprt), su->su_iosz, 0,
  184.                  &(xprt->xp_raddr), &raddr_len );
  185.  
  186.     if (rlen == -1 && errno == WSAEINTR)
  187.         goto again;
  188.     if (rlen < 4*sizeof(u_long))
  189.         return (FALSE);
  190.     xdrs->x_op = XDR_DECODE;
  191.     XDR_SETPOS(xdrs, 0);
  192.     if (! xdr_callmsg(xdrs, msg))
  193.         return (FALSE);
  194.     su->su_xid = msg->rm_xid;
  195.     return (TRUE);
  196. }
  197.  
  198. static bool_t
  199. svcudp_reply(xprt, msg)
  200.     SVCXPRT *xprt; 
  201.     struct rpc_msg *msg; 
  202. {
  203.     register struct svcudp_data *su = su_data(xprt);
  204.     register XDR *xdrs = &(su->su_xdrs);
  205.     register int slen;
  206.     register bool_t stat = FALSE;
  207.  
  208.     xdrs->x_op = XDR_ENCODE;
  209.     XDR_SETPOS(xdrs, 0);
  210.     msg->rm_xid = su->su_xid;
  211.     if (xdr_replymsg(xdrs, msg)) {
  212.         slen = (int)XDR_GETPOS(xdrs);
  213.  
  214. #ifdef OLDSTUFF
  215.         if (sock_send(xprt->xp_sock, &(xprt->xp_raddr),
  216.                 rpc_buffer(xprt), slen) == slen)
  217. #endif
  218.         if (sendto(xprt->xp_sock, rpc_buffer(xprt), slen, 0,
  219.             &(xprt->xp_raddr), sizeof(SOCKADDR_IN)) == slen ) {
  220.  
  221.             stat = TRUE;
  222.         }
  223.     }
  224.     return (stat);
  225. }
  226.  
  227. static bool_t
  228. svcudp_getargs(xprt, xdr_args, args_ptr)
  229.     SVCXPRT *xprt;
  230.     xdrproc_t xdr_args;
  231.     caddr_t args_ptr;
  232. {
  233.  
  234.     return ((*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr));
  235. }
  236.  
  237. static bool_t
  238. svcudp_freeargs(xprt, xdr_args, args_ptr)
  239.     SVCXPRT *xprt;
  240.     xdrproc_t xdr_args;
  241.     caddr_t args_ptr;
  242. {
  243.     register XDR *xdrs = &(su_data(xprt)->su_xdrs);
  244.  
  245.     xdrs->x_op = XDR_FREE;
  246.     (void)((*xdr_args)(xdrs, args_ptr));
  247.     free(args_ptr);
  248.     return(TRUE);
  249. }
  250.  
  251. static void
  252. svcudp_destroy(xprt)
  253.     register SVCXPRT *xprt;
  254. {
  255.     register struct svcudp_data *su = su_data(xprt);
  256.  
  257.     xprt_unregister(xprt);
  258.     (void)_close(xprt->xp_sock);
  259.     XDR_DESTROY(&(su->su_xdrs));
  260.     free(rpc_buffer(xprt));
  261.     mem_free((caddr_t)su, sizeof(struct svcudp_data));
  262.     mem_free((caddr_t)xprt, sizeof(SVCXPRT));
  263. }
  264.